package com.premedsidekick;


import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Research extends ListActivity {
	Button button;
	ArrayAdapter<CharSequence> arrayadapter;
	DefaultDatabaseAdapter dbHelper;
	Cursor cursor;
	String DATABASE_NAME = "research_table";
	DefaultCursorAdapter adapter;
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		dbHelper = new DefaultDatabaseAdapter(this, DATABASE_NAME);
		dbHelper.open();
		
		cursor = dbHelper.getAll();
		/*
		int temp = cursor.getCount();
		System.out.println("# of research entries: " + temp);
		if (temp == 0) {
			for (int i = 0; i < 3; i++) 
				dbHelper.create("Insert name", "0");
			cursor.requery();
		}
		*/
		
		ListView lv = getListView();
		
		button = new Button(this);
		button.setText("Insert new");
		button.setWidth(150);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick (View v) {
				dbHelper.create("Insert name", "0");
				cursor.requery();
			}
		});
		lv.addFooterView(button);
		
		button = new Button(this);
		button.setText("Delete all");
		button.setWidth(150);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				dbHelper.delete();
				cursor.requery();
			}
		});
		lv.addFooterView(button);
		
		adapter = new DefaultCursorAdapter(this, R.layout.research_layout, cursor, dbHelper);
		setListAdapter(adapter);
	}
	public void cursorRefresh() {
		cursor.requery();
	}
	public void onDestroy() {
		super.onDestroy();
		cursor.close();
		dbHelper.close();
	}
		
	public void onStop() {
		super.onStop();
	}
}
